package com.cn.httptest;

import java.io.IOException;

import org.apache.http.HttpHost;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class HttpClientTest {
	
	public static void main(String[] args) throws Exception {
		CloseableHttpClient httpclient = HttpClients.createDefault();      
		HttpGet request =new HttpGet("http://www.baidu.com");
		 
		request.setConfig(
		    RequestConfig.custom()
		        .setProxy(new HttpHost("10.25.36.100",8080,"HTTP"))
		        .build()
		);
		         
		CloseableHttpResponse response = httpclient.execute(request);
		
		int statusCode = response.getStatusLine().getStatusCode();
		System.out.println(statusCode);
		if(statusCode == 200){
			EntityUtils.toString(response.getEntity());
		}
		
		
	}

	public static void post() {
		CloseableHttpClient httpclient = HttpClients.createDefault();
		HttpPost httpPost = new HttpPost("http://www.baidu.com");
		httpPost.setHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) ...");
		String parameter = "key=aaa";
		httpPost.setEntity(new StringEntity(parameter, ContentType.create("application/x-www-form-urlencoded")));
	}

	public static void getTEST() throws IOException, ClientProtocolException {
		CloseableHttpClient httpclient = HttpClients.createDefault();
		HttpGet request =new HttpGet("http://www.baidu.com");
		request.setHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) ...");
		CloseableHttpResponse response = httpclient.execute(request);
		
		StatusLine statusLine = response.getStatusLine();
		int statusCode = statusLine.getStatusCode();
		System.out.println(statusCode);
	}
	

}
